<?php

	/*
	 * AJAX call to load the list of SMS messages.
	 * Uses variables from settings database table, as well as filters (content and phonenumber).
	 *
	 * This version is used for the Flash interface itself.
	 * This way we can have other encodings etc if required.
	 * Also can limit the data traffic.
	 *
	 * Variables:
	 *   id (int) : Record ID of the last already shown SMS
	 *
	 * Returns: xml
	 *
	 */


	header('Content-type: text/xml');

	// Some extra variables
	$begindate = $settings["party_date"]." ".$settings["party_hour"].":00";	// Only messages with datetime > $begindate are used (except for random, see $random_also_old)
	$random_also_old = $settings["sms_use_old"];	// Set to 0 if you don't want random messages to be taken out of the whole database (eg previous party) and only from the begindate...

	$lastd = preg_replace("/\D/","",$begindate);

	$filters_words = array();
	if($settings["ban_by_content"] == 1){
		$sql = mysql_query("SELECT * FROM filterwords ORDER BY fwText", $db);
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_words, $row["fwText"]);
		}
	}

	$filters_numers = array();
	if($settings["ban_by_number"] == 1){
		$sql = mysql_query("SELECT * FROM filternumbers ORDER BY fnNR", $db);
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_numers, $row["fnNR"]);
			array_push($filters_numers, "+".$row["fnNR"]);
		}
	}

	$clean["id"] = round($_GET["id"]);
	$clean["limit"] = round($_GET["lim"]);



		// Get a certain ID and higher, excluding the ID
		$smssql = mysql_query("SELECT * FROM inbox WHERE ID>".$clean["id"]." AND UpdatedInDB > $lastd AND UpdatedInDB < \"".date("YmdHis",time()-$settings["sms_delay"])."\" ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC");
		if($clean["limit"] > 0){
			$temp = mysql_num_rows($smssql);
			$smssql = mysql_query("SELECT * FROM inbox WHERE ID>".$clean["id"]." AND UpdatedInDB > $lastd AND UpdatedInDB < \"".date("YmdHis",time()-$settings["sms_delay"])."\" ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC LIMIT ".max(0, ($temp-$clean["limit"])).", ".$clean["limit"]);
		}

	if(mysql_num_rows($smssql) == 0){
		if($settings["sms_use_old"]){
			$smssql = mysql_query("SELECT * FROM inbox WHERE UpdatedInDB > $lastd AND UpdatedInDB < \"".date("YmdHis",time()-$settings["sms_delay"])."\" ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY RAND() LIMIT ".max(round($settings["sms_use_old_num"]),1));
		}
	}


	echo '<root>';
	$sms = array();
	while($row = mysql_fetch_array($smssql, MYSQL_ASSOC)){
		array_push($sms, $row);
	}
	if(($_GET["dir"] == "up") or ($clean["id"] == 0)){
		$sms = array_reverse($sms);
	}
	foreach($sms as $row){
		echo "\n".'	<sms time="'.substr($row["ReceivingDateTime"], 5, 11).'" id="'.$row["ID"].'" from="'.$row["SenderNumber"].'"><![CDATA['.utf8_encode($row["TextDecoded"]).']]></sms>';
	}
	echo "\n".'</root>';

?>